package com.tiantian.quanminxiu.presenter;

import com.shenglian.utils.utils.CommonUtils;
import com.tiantian.quanminxiu.api.AnchorService;
import com.tiantian.quanminxiu.model.RoomInfo;
import com.tiantian.quanminxiu.utils.ApplicationBase;
import com.tiantian.quanminxiu.utils.Constants;
import com.tiantian.quanminxiu.utils.UserData;
import com.tiantian.quanminxiu.view.SearchView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;

/**
 * Created by lzx on 2016/10/24.
 */

public class SearchPresent implements Presenter<SearchView> {
    private SearchView searchView;
    @Override
    public void attachView(SearchView view) {
        searchView = view;
    }

    @Override
    public void detachView() {
        searchView = null;
    }

    //region 得到热门搜索列表 getHotSearch
    public void getHotSearch(){
        searchView.Loading_Open();
        ApplicationBase app = ApplicationBase.get(searchView.getContext());
        AnchorService service = app.getAnchorService();
        service.listanchorsearch(UserData.Current.mid, Constants.APP_TYPE,UserData.Current.sign,"111",0,true,5,false)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(app.defaultSubscribeScheduler())
                .finallyDo(()->{
                    searchView.Loading_Close();
                })
                .subscribe(str -> {
                    JSONArray AnchorHotList = str.optJSONObject("data").optJSONArray("AnchorHotList");
                    List<RoomInfo> hotList = new ArrayList<RoomInfo>();
                    for (int i=0;AnchorHotList!=null&&i<AnchorHotList.length();i++){
                        RoomInfo roomInfo = new RoomInfo();
                        JSONObject value = AnchorHotList.optJSONObject(i);
                        roomInfo.setAnchorName(value.optString("NickName"));
                        roomInfo.setRoomId(value.optInt("User_ID"));
                        roomInfo.setRanking(i+1);
                        roomInfo.setItemType(RoomInfo.HOT);
                        hotList.add(roomInfo);
                    }
                    searchView.getHotSearch(hotList);
                }, throwable -> {
                    CommonUtils.showToask(searchView.getContext(),throwable.getMessage());
                });

    }
    //endregion

    //region 得到搜索结果列表 getSearchResult
    public void getSearchResult(String value){
        searchView.Loading_Open();
        ApplicationBase app = ApplicationBase.get(searchView.getContext());
        AnchorService service = app.getAnchorService();
        service.listanchorsearch(UserData.Current.mid,Constants.APP_TYPE,UserData.Current.sign,value,15,false,0,true)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(app.defaultSubscribeScheduler())
                .finallyDo(()->{
                    searchView.Loading_Close();
                })
                .subscribe(str -> {
                    JSONArray AnchorList = str.optJSONObject("data").optJSONArray("AnchorList");
                    List<RoomInfo> hotList = new ArrayList<RoomInfo>();
                    for (int i=0;AnchorList!=null&&i<AnchorList.length();i++){
                        JSONObject object = AnchorList.optJSONObject(i);
                        RoomInfo roomInfo = new RoomInfo();
                        roomInfo.setRoomId(object.optInt("User_ID"));
                        roomInfo.setAnchorName(object.optString("NickName"));
                        roomInfo.setHeat(object.optInt("Hot_Value"));
                        roomInfo.setAnchorLevel(object.optInt("Anchor_Level"));
                        roomInfo.setIsPlaying(object.optInt("Live"));
                        roomInfo.setTitle(object.optString("RoomTitle"));
                        roomInfo.setItemType(RoomInfo.COMMON);
                        hotList.add(roomInfo);
                    }
                    searchView.getSearchResult(hotList);
                }, throwable -> {
                    CommonUtils.showToask(searchView.getContext(),throwable.getMessage());
                });
    }
    //endregion

}
